package com.fone.player.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

/**
 * 增强的HorizontalScrollView，可以监听HorizontalScrollView滚动状态
 * 
 * @author kongxiaojun
 * @since 2014-7-10
 */
@SuppressLint("NewApi")
public class EnhanceHorizontalScrollView extends HorizontalScrollView {

	public EnhanceHorizontalScrollView(Context context) {
		super(context);
	}

	public EnhanceHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public EnhanceHorizontalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	private OnScrollListener onScrollListener;

	private boolean isScrolling;
	/**
	 * 主要是用在用户手指离开EnhanceHorizontalScrollView，EnhanceHorizontalScrollView还在继续滑动，
	 * 用来保存X的距离，然后做比较
	 */
	private int lastScrollX;

	/**
	 * 设置滚动接口
	 * 
	 * @param onScrollListener
	 */
	public void setOnScrollListener(OnScrollListener onScrollListener) {
		this.onScrollListener = onScrollListener;
	}

	/**
	 * 用于用户手指离开MyScrollView的时候获取MyScrollView滚动的Y距离，然后回调给onScroll方法中
	 */
	private Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			int scrollX = EnhanceHorizontalScrollView.this.getScrollX();

			// 此时的距离和记录下的距离不相等，在隔5毫秒给handler发送消息
			if (lastScrollX != scrollX) {
				lastScrollX = scrollX;
				handler.sendMessageDelayed(handler.obtainMessage(), 5);
			} else {
				isScrolling = false;
				onScrollListener.onScrollStateChanged(EnhanceHorizontalScrollView.this, OnScrollListener.SCROLL_STATE_STOP);
			}
			if (onScrollListener != null) {
				onScrollListener.onScroll(scrollX);
			}

		};

	};

	/**
	 * 重写onTouchEvent， 当用户的手在EnhanceHorizontalScrollView上面的时候，
	 * 直接将EnhanceHorizontalScrollView滑动的X方向距离回调给onScroll方法中，当用户抬起手的时候，
	 * EnhanceHorizontalScrollView可能还在滑动，所以当用户抬起手我们隔5毫秒给handler发送消息，在handler处理
	 * EnhanceHorizontalScrollView滑动的距离
	 */
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (onScrollListener != null) {
			if (lastScrollX != this.getScaleX() && !isScrolling) {
				isScrolling = true;
				onScrollListener.onScrollStateChanged(this, OnScrollListener.SCROLL_STATE_START);
			}
			onScrollListener.onScroll(lastScrollX = this.getScrollX());
		}
		switch (ev.getAction()) {
		case MotionEvent.ACTION_UP:
			handler.sendMessageDelayed(handler.obtainMessage(), 5);
			break;
		}
		return super.onTouchEvent(ev);
	}

	/**
	 * 滚动的回调接口
	 * 
	 * @author kongxiaojun
	 * @since 2014-7-10
	 */
	public interface OnScrollListener {

		public static final int SCROLL_STATE_STOP = 1;
		public static final int SCROLL_STATE_START = 2;

		/**
		 * 回调方法， 返回EnhanceHorizontalScrollView滑动的X方向距离
		 * 
		 * @param scrollX
		 *            、
		 */
		public void onScroll(int scrollX);

		/**
		 * 回调方法，返回EnhanceHorizontalScrollView滑动状态
		 * 
		 * @param view
		 * @param scrollState
		 * @return void
		 * @throws
		 */
		public void onScrollStateChanged(HorizontalScrollView view, int scrollState);
	}

}
